package com.yyairmarkyy.arctool.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

/**
 * Created by yyairmarkyy on 2017/10/24.
 * 菜单选项layout 负责每个菜单按钮的位置
 */

public class MenuToolLayout extends RelativeLayout {


    private int[] viewsPerRadius;

    public MenuToolLayout(Context context) {
        super(context);
    }

    public MenuToolLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    /**
     * 设置每个child的角度（若不设置则默认为平分后的中间角度）
     * @param viewsPerRadius
     */
    public void setViewsPerRadius(int[] viewsPerRadius) {
        this.viewsPerRadius = viewsPerRadius;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int count = getChildCount();
        int perDegree = 90/count;
        int left;
        int top;
        int finalPerDegree;
        for (int i = 0;i<count;i++){
            View child = getChildAt(i);
            if (viewsPerRadius == null) {
                finalPerDegree = perDegree * (i + 1) - (perDegree / 2);
            }else{
                finalPerDegree = viewsPerRadius[i];
            }
            left = getLeftWithDegress(finalPerDegree);
            top = getTopWithDegress(finalPerDegree);

            child.layout(left,top,left+child.getMeasuredWidth(),top+child.getMeasuredHeight());
        }

    }


    private int getLeftWithDegress(int degree){
        return (int) (getMeasuredWidth()-(Math.sin(Math.toRadians(degree))*getMeasuredWidth()));
    }

    private int getTopWithDegress(int degree){
        return (int) (getMeasuredHeight()-(Math.cos(Math.toRadians(degree))*getMeasuredHeight()));
    }


}
